package com.tang.campus_market.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tang.campus_market.dao.UserDao;
import com.tang.campus_market.entity.User;
import com.tang.campus_market.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    UserDao userdao;

    @Override
    public User getInfoFromEmail(String email) {
        return userdao.getInfoFromEmail(email);
    }


    @Override
    public User getInfoFromName(String name) {
        return userdao.getInfoFromName(name);
    }

    @Override
    public User getInfoFromphoneNumber(String phoneNumber) {
        return userdao.getInfoFromphoneNumber(phoneNumber);
    }

    @Override
    public List<User> findAll() {
        List<User> userList = userdao.findAll();
        //查询到所有用户要把用户的密码全部置null，防止密码泄露
        for (User user : userList) {
            user.setPassword(null);
        }
        return userdao.findAll();
    }

    @Override
    public void deleteUserById(Integer id) {
        //把用户对应的物品状态更改为禁用0
        userdao.updateGoodsStateById(id);

        //把用户对应的评论状态更改为禁用0
        userdao.updateCommentStateById(id);

        //把用户对应的订单状态更改为禁用0
        userdao.updateOrderById(id);

        //从user表中删除
        userdao.deleteById(id);
    }

    @Override
    public void updateUserById(Integer id) {
        //把用户对应的物品状态更改为禁用0
        userdao.updateGoodsStateById(id);

        //把用户对应的评论状态更改为禁用0
        userdao.updateCommentStateById(id);

        //把用户对应的订单状态更改为禁用0
        userdao.updateOrderById(id);

        //从用户状态改为禁用0
        userdao.updateUserById(id);
    }

    @Override
    public void activeUserById(Integer id) {
        //把用户对应的物品状态更改为正常1
        userdao.activeGoodsStateById(id);

        //把用户对应的评论状态更改为正常1
        userdao.activeCommentStateById(id);

        //把用户对应的订单状态更改为正常1
        userdao.activeOrderById(id);

        //从用户状态改为正常1
        userdao.activeUserById(id);
    }

    //根据邮箱和密码查询用户
    @Override
    public User selectUserByEmailAndPassword(String email, String password) {
        return userdao.selectUserByEmailAndPassword(email,password);
    }

    @Override
    public User getById(Integer id) {
        User user = userdao.selectById(id);
        return user;
    }

}
